package com.qding.park.manager.service.core.atomic.distributed;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.qding.park.common.core.constant.CacheKeyParent;
import com.qding.park.common.core.constant.CacheKeyThree;
import com.qding.park.common.core.exceptions.ExceptionMessage;
import com.qding.park.common.core.exceptions.IExceptionHandle;
import com.qding.park.common.core.exceptions.business.manager.service.ServiceCoreException;
import com.qding.park.common.core.redis.JedisClient;
import com.qding.park.common.core.redis.lock.JedisLock;
import com.qding.park.manager.pojo.TbUserInfo;
import com.qding.park.manager.service.core.mapper.TbUserInfoMapper;

@Component("userDBAtomic")
@Scope("singleton")
public class UserDBAtomic {
	
	
	
	@Autowired
	private TbUserInfoMapper tbUserInfoMapper;
	
	@Autowired
	private  JedisClient jedisClient;
	
	@Autowired
	private IExceptionHandle  iExceptionHandle;
	
	
	/**
	 * @param userInfo  用户信息
	 * @return
	 * 注册并登录  最终返回当前　的用户信息
	 */
	public TbUserInfo  register_loginUser(TbUserInfo userInfo){
		if(userInfo==null&&userInfo.getTel()==0){
			return null;
		}
		long tel=userInfo.getTel();
		String lockName = CacheKeyThree.LOCK_DATA_USER_SAVE + CacheKeyParent.SEPARATOR + tel;
		
		JedisLock lock = new JedisLock(jedisClient, lockName, JedisLock.DEFAULT_ACQUIRE_TIMEOUT_MILLIS, 1000*60*1);
		
		try {
			if(lock.acquire()){
				// 得到锁
//				execJob(job);
				TbUserInfo u = tbUserInfoMapper.queryUserByTel(userInfo.getTel());
				if(u==null){
					tbUserInfoMapper.save(userInfo);
					return userInfo;
				}
				
				if(StringUtils.isBlank(userInfo.getBjCode())){
					//数据同步过来的用户数据!此处是可以修改权限字段的(userType)
					tbUserInfoMapper.updateUserTypeBytel(userInfo.getTel(),userInfo.getUserType());
					return u;
				}
				
				if(userInfo.getBjCode().equals(u.getBjCode())){
//					u.setUserType(userInfo.getUserType());
					return u;
				}
				
				if(!userInfo.getBjCode().equals(u.getBjCode())){
					userInfo.setId(u.getId());
					int num = tbUserInfoMapper.updateNotuserType(userInfo);
					u.setBjCode(userInfo.getBjCode());
//					u.setUserType(userInfo.getUserType());
					return u;
					
				}
				
			}else{
				throw new ServiceCoreException(ServiceCoreException.TypeEnum.EXCEPTION_DATA_LOCKED.getId(), null,
						ExceptionMessage.createExceptionMessage(
								ServiceCoreException.TypeEnum.EXCEPTION_DATA_LOCKED.getDesc(), "此用户被锁定请稍候再试",
								" tel=" + tel + ",bjuserId=" + userInfo.getBjCode()));
			}
		} catch (Exception e) {
			iExceptionHandle.handle(e);
			lock.release();
		} finally {
			lock.release();
		}
		return null;
	}
	
//	/**
//	 * @param userInfo
//	 * 以tel 为锁分布式　不存在tel 则保存用户信息
//	 */
//	public boolean saveUserAtomicTelNotExists(TbUserInfo userInfo){
//		if(userInfo==null&&userInfo.getTel()==0){
//			return false;
//		}
//		
//		long tel=userInfo.getTel();
//		String lockName = CacheKeyThree.LOCK_DATA_USER_SAVE + CacheKeyParent.SEPARATOR + tel;
//		
//		JedisLock lock = new JedisLock(jedisClient, lockName, JedisLock.DEFAULT_ACQUIRE_TIMEOUT_MILLIS, 1000*60*5);
//		try {
//			if(lock.acquire()){
//				// 得到锁
////				execJob(job);
//				TbUserInfo u = tbUserInfoMapper.queryUserByTel(userInfo.getTel());
//				if(u==null){
//					tbUserInfoMapper.save(userInfo);
//					return true;
//				}
//				
//				return false;
//			}
//		} catch (Exception e) {
//			iExceptionHandle.handle(e);
//			lock.release();
//		} finally {
//			lock.release();
//		}
//		
//		return false;
//	}
}
